package com.qf.openapi.web.master.controller;

import com.qf.openapi.web.master.utils.Constants;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Controller
public class DownloadController {
    @RequestMapping("/download")
    public void download(String filename, HttpServletRequest request, HttpServletResponse response){
        // 返回服务器端生成好的文件
        final String realPath = request.getServletContext().getRealPath(Constants.UPLOAD_FILE_PATH);
        try {
            response.addHeader("content-disposition","attachment;filename="+filename);
            IOUtils.copy(new FileInputStream(realPath+ File.separator+filename),response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
